package com.yanqu.road.server.cross3server.cmd.xrank.season;

import com.yanqu.road.entity.rank.xrank.XSeasonUserRank;
import com.yanqu.road.logic.helper.RankHelper;
import com.yanqu.road.logic.pb.XRankPb;
import com.yanqu.road.logic.xlogic.rank.season.XSeasonUserRankListModel;
import com.yanqu.road.logic.xlogic.rank.season.XSeasonUserRankMap;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.cross3server.manager.Cross3XRankMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = Cross3Protocol.C3_XRANK_GET_SEASON_USER_RANK_LIST, desc = "查看赛季个人榜单")
public class XRankGetSeasonUserRankListFromGameCmd extends CrossCommand {

    private int systemId;
    private int seasonId;
    private int type;
    private String rangeKey;
    private long myValue;
    private int pageNo;
    private int pageSize;

    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        XRankProto.CrossGetXSeasonUserRankListReqMsg reqMsg = XRankProto.CrossGetXSeasonUserRankListReqMsg.parseFrom(packet.getBytes());
        systemId = reqMsg.getSystemId();
        seasonId = reqMsg.getSeasonId();
        type = reqMsg.getType();
        rangeKey = reqMsg.getRangeKey();
        myValue = reqMsg.getMyValue();
        pageNo = reqMsg.getPageNo();
        pageSize = reqMsg.getPageSize();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        XSeasonUserRankMap seasonUserRankMap = Cross3XRankMgr.getSeasonUserRankMap();
        XSeasonUserRankListModel seasonUserRankListModel = seasonUserRankMap.getUserRankListModel(systemId, seasonId, type, rangeKey);

        // 获取分页榜单
        List<XSeasonUserRank> allRankList = seasonUserRankListModel.getAllRankList();
        List<XSeasonUserRank> pageRankList = RankHelper.getPageRankList(allRankList, seasonUserRankListModel.getMaxRank(), pageNo, pageSize);

        XRankProto.XSeasonUserRankListMsg.Builder rankListMsg = XRankPb.parseXSeasonUserRankListMsg(systemId, seasonId, type, rangeKey, pageRankList, false);
        int myRank = seasonUserRankListModel.getMyRank(userId);

        XRankProto.GetXSeasonUserRankListRespMsg.Builder builder = XRankProto.GetXSeasonUserRankListRespMsg.newBuilder();
        builder.setRet(0);
        XRankPb.fillFlagId(rankListMsg);
        builder.setRankList(rankListMsg);
        builder.setMyRank(myRank);
        builder.setMyValue(myValue);
        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_XRANK_GET_SEASON_USER_RANK_LIST, builder);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
